home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / schmlbrr / schem_lb.lha / unsupported / CScheme / btree.scm < prev    next >
Encoding:
Text File  |  1993-07-16  |  8.2 KB  |  260 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Header: btree.scm,v 1.2 88/10/28 07:03:00 GMT cph Rel $
  4.  
  5. Copyright (c) 1988 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. |#
  34.  
  35. ;;;; Balanced Trees
  36. ;;;  Knuth, Donald E., "The Art of Computer Programming",
  37. ;;;  volume 3, "Sorting and Searching",
  38. ;;;  section 6.2.3, "Balanced Trees".
  39.  
  40. (declare (usual-integrations))
  41.  
  42. (define-integrable (make-node wrapped-key left-link right-link balance-factor)
  43.   (vector wrapped-key left-link right-link balance-factor))
  44.  
  45. (define-integrable (get-wrapped-key node) (vector-ref node 0))
  46. (define-integrable (set-wrapped-key! node key) (vector-set! node 0 key))
  47.  
  48. (define-integrable (get-left-link node) (vector-ref node 1))
  49. (define-integrable (set-left-link! node link) (vector-set! node 1 link))
  50.  
  51. (define-integrable (get-right-link node) (vector-ref node 2))
  52. (define-integrable (set-right-link! node link) (vector-set! node 2 link))
  53.  
  54. (define-integrable (get-balance-factor node) (vector-ref node 3))
  55. (define-integrable (set-balance-factor! node b) (vector-set! node 3 b))
  56.  
  57. (define-integrable balanced 'BALANCED)        ; Knuth's 0
  58. (define-integrable tipped-left 'TIPPED-LEFT)    ; Knuth's -1
  59. (define-integrable tipped-right 'TIPPED-RIGHT)    ; Knuth's +1
  60.  
  61. (define left-d (vector tipped-left tipped-right 1 2))
  62. (define right-d (vector tipped-right tipped-left 2 1))
  63.  
  64. (define (-d d)
  65.   (cond ((eq? d left-d) right-d)
  66.     ((eq? d right-d) left-d)
  67.     (else (error "-D: Bad argument" d))))
  68.  
  69. (define-integrable (+a d)
  70.   (vector-ref d 0))
  71.  
  72. (define-integrable (-a d)
  73.   (vector-ref d 1))
  74.  
  75. (define-integrable (get-link+ p d)
  76.   (vector-ref p (vector-ref d 2)))
  77.  
  78. (define-integrable (get-link- p d)
  79.   (vector-ref p (vector-ref d 3)))
  80.  
  81. (define-integrable (set-link+! p d l)
  82.   (vector-set! p (vector-ref d 2) l))
  83.  
  84. (define-integrable (set-link-! p d l)
  85.   (vector-set! p (vector-ref d 3) l))
  86.  
  87. (define-integrable (cons-path p d path)
  88.   (list p d path))
  89.  
  90. (define-integrable (path-components path receiver)
  91.   (apply receiver path))
  92.  
  93. (define (make-btree)
  94.   (make-node 'DUMMY-KEY '() '() balanced))
  95.  
  96. (define (btree-insert! btree < unwrap-key k wrap-key if-found if-not-found)
  97.   (let ((p (get-right-link btree)))
  98.     (if (null? p)
  99.     (let ((wk (wrap-key k)))
  100.       (set-right-link! btree (make-node wk '() '() balanced))
  101.       (if-not-found wk))
  102.     (let search ((t btree) (set-s-link! set-right-link!) (s p) (p p))
  103.       (define (move-once set-link! q)
  104.         (cond ((null? q)
  105.            (let ((wk (wrap-key k)))
  106.              (let ((q (make-node wk '() '() balanced)))
  107.                (set-link! p q)
  108.                (let ((d (if (< k (unwrap-key (get-wrapped-key s)))
  109.                     left-d
  110.                     right-d)))
  111.              (let adjust-balance-factors! ((p (get-link+ s d)))
  112.                (cond ((eq? p q) 'DONE)
  113.                  ((< k (unwrap-key (get-wrapped-key p)))
  114.                   (set-balance-factor! p tipped-left)
  115.                   (adjust-balance-factors!
  116.                    (get-left-link p)))
  117.                  (else
  118.                   (set-balance-factor! p tipped-right)
  119.                   (adjust-balance-factors!
  120.                    (get-right-link p)))))
  121.              (cond ((eq? (get-balance-factor s) balanced)
  122.                 (set-balance-factor! s (+a d)))
  123.                    ((eq? (get-balance-factor s) (-a d))
  124.                 (set-balance-factor! s balanced))
  125.                    (else
  126.                 (rebalance! s d
  127.                   (lambda (new-s)
  128.                     (set-s-link! t new-s))
  129.                   (lambda (new-s)
  130.                     new-s
  131.                     (error "Tree shouldn't be same height!"
  132.                        'BTREE-INSERT!)))))))
  133.              (if-not-found wk)))
  134.           ((eq? (get-balance-factor q) balanced)
  135.            (search t set-s-link! s q))
  136.           (else
  137.            (search p set-link! q q))))
  138.       (let ((kp (unwrap-key (get-wrapped-key p))))
  139.         (cond ((< k kp)
  140.            (move-once set-left-link! (get-left-link p)))
  141.           ((< kp k)
  142.            (move-once set-right-link! (get-right-link p)))
  143.           (else
  144.            (if-found (get-wrapped-key p)))))))))
  145.  
  146. (define (btree-delete! btree < unwrap-key k if-found if-not-found)
  147.   (let loop ((p (get-right-link btree))
  148.          (path (cons-path btree right-d '())))
  149.     (if (null? p)
  150.     (if-not-found k)
  151.     (let ((kp (unwrap-key (get-wrapped-key p))))
  152.       (cond ((< k kp)
  153.          (loop (get-left-link p)
  154.                (cons-path p left-d path)))
  155.         ((< kp k)
  156.          (loop (get-right-link p)
  157.                (cons-path p right-d path)))
  158.         (else
  159.          (let ((result (get-wrapped-key p)))
  160.            (cond ((null? (get-left-link p))
  161.               (replace-node! path (get-right-link p)))
  162.              ((null? (get-right-link p))
  163.               (replace-node! path (get-left-link p)))
  164.              (else
  165.               (set-wrapped-key!
  166.                p
  167.                (get-wrapped-key
  168.                 (remove-successor! (get-right-link p)
  169.                            (cons-path p right-d path))))))
  170.            (if-found result))))))))
  171.  
  172. (define (btree-lookup btree < unwrap-key k if-found if-not-found)
  173.   (let loop ((p (get-right-link btree)))
  174.     (if (null? p)
  175.     (if-not-found k)
  176.     (let ((kp (unwrap-key (get-wrapped-key p))))
  177.       (cond ((< k kp)
  178.          (loop (get-left-link p)))
  179.         ((< kp k)
  180.          (loop (get-right-link p)))
  181.         (else
  182.          (if-found (get-wrapped-key p))))))))
  183.  
  184. (define (btree-fringe btree)
  185.   (let loop ((p (get-right-link btree)) (tail '()))
  186.     (if (null? p)
  187.     tail
  188.     (loop (get-left-link p)
  189.           (cons (get-wrapped-key p)
  190.             (loop (get-right-link p) tail))))))
  191.  
  192. (define (remove-successor! p path)
  193.   (if (null? (get-left-link p))
  194.       (begin (replace-node! path (get-right-link p))
  195.          p)
  196.       (remove-successor! (get-left-link p)
  197.              (cons-path p left-d path))))
  198.  
  199. (define (replace-node! path new-node)
  200.   (path-components path
  201.     (lambda (pl-1 dl-1 rest)
  202.       (set-link+! pl-1 dl-1 new-node)
  203.       (adjust-balance-factors! pl-1 dl-1 rest))))
  204.  
  205. (define (adjust-balance-factors! pk dk path)
  206.   (cond ((null? path) 'DONE)
  207.     ((eq? (get-balance-factor pk) balanced)
  208.      (set-balance-factor! pk (-a dk)))
  209.     (else
  210.      (path-components path
  211.        (lambda (pk-1 dk-1 rest)
  212.          (if (eq? (get-balance-factor pk) (+a dk))
  213.          (begin (set-balance-factor! pk balanced)
  214.             (adjust-balance-factors! pk-1 dk-1 rest))
  215.          (rebalance! pk (-d dk)
  216.                  (lambda (new-pk)
  217.                    (set-link+! pk-1 dk-1 new-pk)
  218.                    (adjust-balance-factors! pk-1 dk-1 rest))
  219.                  (lambda (new-pk)
  220.                    (set-link+! pk-1 dk-1 new-pk)))))))))
  221.  
  222. (define (rebalance! A d if-shorter if-same-height)
  223.   (let ((B (get-link+ A d)))
  224.     (define (case-1)
  225.       (set-link+! A d (get-link- B d))
  226.       (set-balance-factor! A balanced)
  227.       (set-link-! B d A)
  228.       (set-balance-factor! B balanced)
  229.       (if-shorter B))
  230.  
  231.     (define (case-2 X)
  232.       (set-link-! B d (get-link+ X d))
  233.       (set-link+! X d B)
  234.       (set-link+! A d (get-link- X d))
  235.       (set-link-! X d A)
  236.       (cond ((eq? (get-balance-factor X) balanced)
  237.          (set-balance-factor! A balanced)
  238.          (set-balance-factor! B balanced))
  239.         ((eq? (get-balance-factor X) (+a d))
  240.          (set-balance-factor! A (-a d))
  241.          (set-balance-factor! B balanced))
  242.         (else
  243.          (set-balance-factor! A balanced)
  244.          (set-balance-factor! B (+a d))))
  245.       (set-balance-factor! X balanced)
  246.       (if-shorter X))
  247.  
  248.     (define (case-3)
  249.       (set-link+! A d (get-link- B d))
  250.       (set-balance-factor! A (+a d))
  251.       (set-link-! B d A)
  252.       (set-balance-factor! B (-a d))
  253.       (if-same-height B))
  254.  
  255.     (cond ((eq? (get-balance-factor B) (+a d))
  256.        (case-1))
  257.       ((eq? (get-balance-factor B) (-a d))
  258.        (case-2 (get-link- B d)))
  259.       (else
  260.        (case-3)))))